package com.csun.cmny.provider.utils;

import com.csun.cmny.provider.model.vo.rfid.RfidLabelRoleVo;
import com.csun.cmny.util.PublicUtil;
import com.google.common.collect.Lists;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Objects;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-08-12 20:10
 * @Description: tree util
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class TreeUtil {

    public static List<RfidLabelRoleVo> getChildLabelRoleVos(List<RfidLabelRoleVo> list, Long parentId) {

        List<RfidLabelRoleVo> returnList = Lists.newArrayList();

        for (RfidLabelRoleVo deptVo : list) {
            if (deptVo.getPid() == null) {
                continue;
            }

            if (Objects.equals(deptVo.getPid(), parentId)) {
                recursionFnDept(list, deptVo);
                returnList.add(deptVo);
            }
        }
        return returnList;

    }

    private static void recursionFnDept(List<RfidLabelRoleVo> list, RfidLabelRoleVo t) {
        // 得到子节点列表
        List<RfidLabelRoleVo> childList = getChildRfidLabelList(list, t);
        t.setChildren(childList);
        if (PublicUtil.isNotEmpty(childList)) {
            t.setHasChild(true);
        }
        for (RfidLabelRoleVo tChild : childList) {
            // 判断是否有子节点
            if (hasChild(list, tChild)) {
                for (RfidLabelRoleVo n : childList) {
                    recursionFnDept(list, n);
                }
                tChild.setHasChild(true);
            }
        }
    }

    private static List<RfidLabelRoleVo> getChildRfidLabelList(List<RfidLabelRoleVo> list, RfidLabelRoleVo t) {
        List<RfidLabelRoleVo> tList = Lists.newArrayList();

        for (RfidLabelRoleVo deptVo : list) {
            if (PublicUtil.isEmpty(deptVo.getPid())) {
                continue;
            }
            if (Objects.equals(deptVo.getPid(), t.getId())) {
                tList.add(deptVo);
            }
        }
        return tList;
    }

    /**
     * 判断是否有子节点
     */
    private static boolean hasChild(List<RfidLabelRoleVo> list, RfidLabelRoleVo t) {

        return !getChildRfidLabelList(list, t).isEmpty();
    }

}
